package swordoffer.offer36;

public class Debug {

    public static void main(String[] args) {
        Node root = new Node(4);
        root.left = new Node(2);
        root.right = new Node(5);
        root.left.left = new Node(1);
        root.left.right = new Node(3);
        treeToDoublyList(root);

    }
    public static Node treeToDoublyList(Node root) {
        Node head = null;
        Node pre = null;
        dfs(root, head, pre);
        head.left = pre;
        pre.right = head;
        return head;
    }
    public static void dfs(Node root, Node head, Node pre){
        if(root == null){
            return;
        }
        dfs(root.left, head, pre);
        if(pre == null){
            head = root;
        }else{
            pre.right = root;
        }
        root.left = pre;
        pre = root;
        dfs(root.right, head, pre);
    }
}
